package com.imodia.checkoo.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.hsq.commons.util.AuthConfig;
import com.imodia.checkoo.bean.CmsManagerMenuBean;
import com.imodia.checkoo.bean.CmsUserBean;
import com.imodia.checkoo.service.impl.CmsManagerMenuServiceImpl;

public class PrivateTag extends RequestContextAwareTag{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7936918965893249137L;

	private static final Log log = LogFactory.getLog(ListTag.class);
	
	private int pmenuId;

	public int getPmenuId() {
		return pmenuId;
	}

	public void setPmenuId(int pmenuId) {
		this.pmenuId = pmenuId;
	}


	@Override
	protected int doStartTagInternal() throws Exception {
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		ApplicationContext ctx = getRequestContext().getWebApplicationContext();
		CmsManagerMenuServiceImpl cmsManagerMenuService = (CmsManagerMenuServiceImpl)ctx.getBean("cmsManagerMenuService");
		List<CmsManagerMenuBean> navigateMenuList = null;
		if(getDepartment(request)==1){
			navigateMenuList = cmsManagerMenuService.queryNavigateMenuList(pmenuId, 1);
		}else{
			navigateMenuList = cmsManagerMenuService.queryPopedomByDepartment(pmenuId, getDepartment(request));
		}
		request.setAttribute("prvMn", navigateMenuList);
		return 0;
	}
	
	public int getDepartment(HttpServletRequest request){
		Object o = request.getSession().getAttribute(AuthConfig.SESSION_USER_INFO);
		if(o!=null){
			 CmsUserBean user = (CmsUserBean)o;
			 log.info("___%%%departmentId:"+user.getDepartmentId());
			 return  user.getDepartmentId();
		}
		return 0;
	}
	
}
